<?php

class Controller_Home extends Controller_Template
{
    public function before()
    {
        parent::before();
    }

    public function action_login()
    {
        $user_info = null;
        $auth = \Auth\Auth::instance();
        if(!\Auth\Auth::check())
        {
            if(Fuel\Core\Input::post())
            {
                if($auth->login(Fuel\Core\Input::post('username'), Fuel\Core\Input::post('password')))
                {
                    Fuel\Core\Session::set_flash('success', 'Đăng nhập thành công');
                    $user_info = $auth->get_screen_name();
                    Fuel\Core\Response::redirect('/');
                }
                else
                {
                    \Fuel\Core\Session::set_flash('error', 'Đăng nhập không thành công');
                }
            }
                $this->template->subnav = array('login' => 'active');
                $this->template->title = 'STUDENT';
                $this->template->content = View::forge('home/login');
        }else
        {
            Fuel\Core\Response::redirect('/');
        }
    }

    public function action_logout()
    {      
        if(\Auth\Auth::check())
        {
            $auth = \Auth\Auth::instance();
            $auth->logout();
            \Fuel\Core\Session::set_flash('success', 'Bạn đã đăng xuất');
            Fuel\Core\Response::redirect('/');
        }else
        {
            Fuel\Core\Response::redirect('/home/login');
        }
    }

    public function action_register()
    {
        if(!Auth\Auth::check())
        {
            $data = array();
            $auth = \Auth\Auth::instance();
            if(Fuel\Core\Input::method() == 'POST')
            {

                $val = Model_Member::validate_register('create');

                if($val->run())
                {
                    $member = Model_Member::forge(Fuel\Core\Input::post());
                    try
                    {
                        $user = $auth->create_user($member->username, $member->password, $member->email);
                    } catch (Exception $ex)
                    {
                        \Fuel\Core\Session::set_flash('error', $ex->getMessage());
                    }

                    if(isset($user))
                    {
                        $auth->login($member->username, $member->password);
                        $user_info = $auth->get_screen_name();
                        Fuel\Core\Response::redirect('/');
                    }
                    else
                    {
                        \Fuel\Core\Session::set_flash('error', 'Đăng nhập không thành công');
                    }
                }else
                {
                    $data['valid'] = $val->error();
                }
            }
            $this->template->subnav = array('register' => 'active');
            $this->template->title = 'STUDENT';
            $this->template->content = View::forge('home/register', $data);
        }
        else
        {
            \Fuel\Core\Response::redirect('/');
        }
    }

    public function action_forget_password()
    {
            $this->template->subnav = array('login' => 'active');
            $this->template->title = 'STUDENT';
            $this->template->content = View::forge('home/forget_password');
    }

    public function action_index()
    {
        $data['subnav'] = "home";
        $this->template->title = "Student - trang chủ";
        $this->template->content = \Fuel\Core\View::forge('home/index', $data);
    }

}
